<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\ExcelAdd;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';


    public function setting(Content $content)
    {
        return $content
            ->title('网站设置')
            ->body(new Setting());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->disableExport();
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器

            $filter->column(1/2,function ($filter){
                $filter->like('mobile','电话号码');
            });

            $filter->column(1/2,function ($filter){
                //$filter->equal('company_id','所属公司')->select($company);
                $filter->like('name','姓名');

            });

        });

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id'));
        $grid->column('nickname',"昵称");
        $grid->column('header_img',"头像")->image('',50,50);
        $grid->column('name',"姓名");
        $grid->column('mobile',"联系方式");
        //$grid->column('money',"激励金");
//        $grid->column('company.name',"所属公司")->modal('公司信息',function ($model){
//            if($model->company_id){
//                $show = new Show(Company::findOrFail($model->company_id));
//                $show->field('code', __('邀请码'));
//                $show->field('name', __('名称'));
//                $show->field('address', __('地址'));
//                $show->field('created_at', __('添加时间'));
//
//                $show->panel()
//                    ->tools(function ($tools) {
//                        $tools->disableEdit();
//                        $tools->disableList();
//                        $tools->disableDelete();
//                    });
//                return $show;
//            }
//
//        });
        //$grid->column('work_number',"工号");
//        $grid->column('status',"审核状态")->radio([
//            0=>'待审核',
//            1 => '审核通过',
//            2 => '审核失败'
//        ]);
//        $grid->column('work_status',"在职状态")->radio([
//            1=>'在职',
//            2 => '离职',
//            3 => '注销'
//        ]);


        $grid->column('updated_at',"更新时间");
        $grid->column('created_at',"创建时间");

        $grid->actions(function($actions){

            $actions->disableView();
            $actions->disableDelete();

        });
        $grid->tools(function (Grid\Tools $tools) {
            // excle 导入
            $tools->append(new ExcelAdd());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('nickname', __('Nickname'));
        $show->field('mobile', __('Mobile'));
        $show->field('header_img', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('姓名'));
        $form->text('mobile', __('电话'));
        $form->text('nickname', __('昵称'));


        $form->image('header_img', __('头像'))->removable();

//        $form->saved(function (Form $form) {
//
//            $user_id = $form->model()->id;
//
//            $user = DB::table('users')->find($user_id);
//            if(($form->model()->status==1||$form->model()->status==2)&&$user->status != $form->model()->status){
//
//                $wechat = new WechatController();
//
//                $wechat->send_msg($user_id); //保存成功 发送订阅消息
//            }
//
//
//        });


        $form->footer(function ($footer) {

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }
}
